package com.treasure.hunt.api

import com.blankj.utilcode.util.GsonUtils
import com.module.lemlin.mode.SubscribeListener
import com.module.lemlin.rxhttp.HttpServiceData
import com.treasure.hunt.api.subscribe.subscribe
import com.treasure.hunt.entity.*
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.Disposable
import rxhttp.RxHttp


/**
 * 获取商品
 * */
fun indexGetGoods(
    request: GoodsRequest, listener: SubscribeListener<GoodsResponse>
): Disposable = subscribe(
    Observable.just(request)
        .flatMap {
            val maps: Map<String, String?> =
                GsonUtils.fromJson<Map<String, String?>>(
                    GsonUtils.toJson(request),
                    Map::class.java
                ).filter { it.value != null }
            Observable.just(maps)
        }.flatMap {
            RxHttp.get("/api/index/get_goods")
                .addAll(it)
                .asClass(GoodsResponse::class.java)
        }
    , listener
)

/**
 * 收藏
 * */
fun indexAddGoodsCollection(
    goodsId: Int, listener: SubscribeListener<GoodsCollectionResponse>
): Disposable = subscribe(
    RxHttp.postForm("/api/index/add_goods_collection")
        .add("goods_id", goodsId)
        .asClass(GoodsCollectionResponse::class.java)
    , listener
)

/**
 * 获取分类
 * */
fun indexGetCategory(
    listener: SubscribeListener<CategoryResponse>
): Disposable = subscribe(
    RxHttp.get("/api/index/get_category")
        .asClass(CategoryResponse::class.java)
    , listener
)

/**
 * 获取默认搜索词
 * */
fun indexGetSearchWords(
    listener: SubscribeListener<SearchResponse>
): Disposable = subscribe(
    RxHttp.get("/api/index/get_search_words")
        .asClass(SearchResponse::class.java)
    , listener
)

/**
 * 合成商品
 * */
fun indexSynthesisGoods(
    goodsId: Int, listener: SubscribeListener<SynthesisResponse>
): Disposable = subscribe(
    RxHttp.postForm("/api/index/synthesis_goods")
        .add("goods_id", goodsId)
        .asClass(SynthesisResponse::class.java)
    , listener
)

/**
 * 合成记录
 * */
fun indexGetOrderLog(
    page: Int, limit:Int, listener: SubscribeListener<MergeResponse>
): Disposable = subscribe(
    RxHttp.get("/api/index/get_order_log")
        .add("page", page)
        .add("limit", limit)
        .asClass(MergeResponse::class.java)
    , listener
)

/**
 * 获取公告
 * */
fun indexGetNotice(
    page: Int = 1, limit: Int = 20, listener: SubscribeListener<NoticeResponse>
): Disposable = subscribe(
    RxHttp.get("/api/index/get_notice")
        .add("page", page)
        .add("limit", limit)
        .asClass(NoticeResponse::class.java)
    , listener
)